package com.zs.toolbox.web2j.system.message.controller;

import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.system.message.domain.request.WtSendEmailRequest;
import com.zs.toolbox.web2j.system.message.domain.request.WxSendSmsVcodeRequest;
import com.zs.toolbox.web2j.system.message.domain.service.WtMessageClientService;
import com.zs.toolbox.web2j.system.message.mq.WtMessageProduct;
import com.zs.toolbox.web2j.system.message.service.WtEmailService;
import com.zs.toolbox.web2j.system.message.service.WtSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shuai.zhang  on 2018/9/22
 * @description
 */
@RestController
public class WtStorageController extends WtBaseController implements WtMessageClientService {

    @Autowired
    WtEmailService wtEmailService;

    @Autowired
    WtSmsService wtSmsService;

    @Autowired
    WtMessageProduct messageProduct;

    public WtBaseResponse sendEmail(@RequestBody WtSendEmailRequest request) {
        this.volidate(request);

        if (!request.getIsSync()) {
            messageProduct.sendEmailAsync(request);
        } else {
            wtEmailService.sendAttachmentsEmail(request.getEmails()
                    , request.getTitle(), request.getContent(), null);
        }
        return WtResponses.SUCCESS;
    }

    @Override
    public WtBaseResponse sendPhoneRegisterVCodeSms(@RequestBody WxSendSmsVcodeRequest request) {
        this.volidate(request);
        if (request.getIsSync()) {
            if (!wtSmsService.sendRegisterVCodeSms(request.getPhone(), request.getVcode())) {
                throw WtExceptions.E_MESSAGE_SMS_SEND_ERROR;
            }
        } else {
            messageProduct.sendSmsRegisterVcodeAsync(request);
        }
        return WtResponses.SUCCESS;
    }

    @Override
    public WtBaseResponse sendPhoneLoginVCodeSms(@RequestBody WxSendSmsVcodeRequest request) {
        this.volidate(request);
        if (request.getIsSync()) {
            if (!wtSmsService.sendLoginVCodeSms(request.getPhone(), request.getVcode())) {
                throw WtExceptions.E_MESSAGE_SMS_SEND_ERROR;
            }
        } else {
            messageProduct.sendSmsLoginVcodeAsync(request);
        }
        return WtResponses.SUCCESS;
    }

    @Override
    public WtBaseResponse sendPhoneFindPasswordVCodeSms(@RequestBody WxSendSmsVcodeRequest request) {
        this.volidate(request);
        if (request.getIsSync()) {
            if (!wtSmsService.sendFindPasswordSms(request.getPhone(), request.getVcode())) {
                throw WtExceptions.E_MESSAGE_SMS_SEND_ERROR;
            }
        } else {
            messageProduct.sendSmsFindPasswordVcodeAsync(request);
        }
        return WtResponses.SUCCESS;
    }
}
